<?php
/**
 * 分类
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\GoodsCategory;
use think\Db;

class Category extends Api {
	
	protected $noNeedLogin = ['*'];
	
	/**
	 * 推荐分类
	 */
	public function recommend(){
		$rows = Db::name('goodsCategory')->order(['sort' => 'asc'])
			->field('id, name, image, link_type, link_url')
			->where(['status' => GoodsCategory::STATUS_Y])
			->where('delete_time', 'NULL')
			->order(['sort' => 'asc'])
			->select();
		
		$this->success(__('Select success'), $rows);
	}
	
	/**
	 * 获取顶级
	 */
	public function index(){
		$model = new GoodsCategory();
		$data = $model->getListById(0);
		$this->success('查询成功', $data);
	}
	
	/**
	 * 获取下级分类
	 */
	public function getSonCategory(){
		$id = input('post.id');
		if(!$id) {
			$this->error('参数错误');
		}
		
		$model = new GoodsCategory();
		$sons = $model->getListById($id);
		$returnData = [];
		if($sons) {
			foreach($sons as $son){
				$son['son_category'] = $model->getListById($son['id']);
				$returnData[] = $son;
			}
		}
		$this->success('查询成功', $returnData);
	}
}
